package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import sz.szsmk.citizencard.R;

/**
 * author : hyt
 * date : 2019/8/19
 * description :
 */
public class CalenderForPopWindowAdapter extends RecyclerView.Adapter<CalenderForPopWindowAdapter.MyViewHolder>{

    private Context context;

    private List<String> arr = new ArrayList<>();
    private List<String> arrResult = new ArrayList<>();
    private OnItemClickListener onItemClickListener;


    public CalenderForPopWindowAdapter(Context context){
        this.context = context;

        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM");//设置日期格式
        SimpleDateFormat dfResult = new SimpleDateFormat("yyyy-MM");//设置日期格式
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());

        for(int i=0;i<12;i++){
            Date m = c.getTime();
            arr.add(df.format(m));
            c.add(Calendar.MONTH, -1);
            arrResult.add(dfResult.format(m));
        }

    }

    @Override
    public CalenderForPopWindowAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_calender, parent, false);
        return new CalenderForPopWindowAdapter.MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CalenderForPopWindowAdapter.MyViewHolder holder, int position) {

        if(position==0){
            holder.tv_content.setText("全部");
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick("");
                }
            });

        }else{
            holder.tv_content.setText(arr.get(position-1));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(arrResult.get(position-1));
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        return arr.size()+1;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_content;

        MyViewHolder(View itemView) {
            super(itemView);
            tv_content = itemView.findViewById(R.id.tv_content);
        }
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(String data);
    }
}
